﻿/**************************************************************

项目名称 :ThinkWide数据访问基础库
版权所有 (c) 2010 ThinkWide
+-------------------------------------------------------------
项目: DataService数据库服务类


版本: 1


创建日期:2009-7-1


作者: 章称

+-------------------------------------------------------------
描述:

更新:   2010-8-1
 * 新增对WCF的支持

ToDo:

***************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MF.Data.Manager;
using Wcf=MF.Data.ORMapping.MFWcfServiceReference;
using Web=MF.Data.ORMapping.MFWebServiceReference;
using MF.Data.SupplyInterface;

namespace MF.Data.ORMapping
{
    /// <summary>
    /// 通过本地直连
    /// </summary>
    public class MFDirectDataService:IDataService
    {
        private MFDataManager dataManager;

        public MFDirectDataService()
        {
            dataManager = new MFDataManager();
        }

        private string ParameterKey;
        private string OriginalKey;

        public string GetParameterKey()
        {
            return ParameterKey;
        }

        public string GetOriginalKey()
        {
            return OriginalKey;
        }

        public bool Init()
        {
            return true;
        }
        public bool Init(string connectString)
        {
            return Init();
        }

        public DataSet GetDataSet(string sql)
        {
            return dataManager.GetDataSet(sql);
        }

        public int SaveDataTable(DataTable dataTable)
        {
            dataTable=dataManager.UpdateTable(dataTable, dataTable.TableName);
            return 0;
        }

        public int ExecuteSQL(string sql)
        {
            return dataManager.ExecuteSQL(sql);
        }

        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleInfo info = new MFDBResuleInfo();
            MFDataManager dm = new MFDataManager();
            try
            {
                
                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;

                if (dataParameters != null)
                foreach (MFDataParameter param in dataParameters)
                {

                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);

                    cmd.Parameters.Add(dp);
                }

                cmd.Prepare();

                info.Count = cmd.ExecuteNonQuery();

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                        dataParameters[param.ParameterName].Value = param.Value;
                }
                info.DataParameters = dataParameters;
                info.IsSuccess = true;
                info.Code = 0;
                dm.Close();
                return info;
            }
            catch (Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            MFDBResuleDataSetInfo info = new MFDBResuleDataSetInfo();
            MFDataManager dm = new MFDataManager();
            try
            {

                IDbCommand cmd = dm.GetCommand(true);
                cmd.CommandType = commandType;
                cmd.CommandText = sql;
                if (dataParameters!=null)
                foreach (MFDataParameter param in dataParameters)
                {

                    IDataParameter dp = dm.GetParameter(param.ParameterName, param.DbType, param.Size);
                    param.Assign(dp);

                    cmd.Parameters.Add(dp);
                }

                IDataAdapter da = dm.GetDataAdapter(cmd);

                cmd.Prepare();

                DataSet ds = new DataSet();
                dm.CheckedOpen();
                da.Fill(ds);
                if (ds.Tables.Count > 0)
                    info.Count = ds.Tables[0].Rows.Count;
                else
                    info.Count = 0;

                if (dataParameters != null)
                foreach (IDataParameter param in cmd.Parameters)
                {
                    if (param.Direction != ParameterDirection.Input)
                        dataParameters[param.ParameterName].Value = param.Value;
                }

                info.DataParameters = dataParameters;
                info.ResultDataSet = ds;
                info.IsSuccess = true;
                info.Code = 0;
                dm.Close();
                return info;
            }
            catch (Exception ex)
            {
                info.IsSuccess = false;
                info.Message = ex.Message;
                info.Count = -1;
                dm.Close();
                return info;
            }
        }
    }

    /// <summary>
    /// 通过WebService服务获取数据
    /// </summary>
    public class MFWebDataService : IDataService
    {
        public bool Init()
        {
            return true;
        }
        public bool Init(string connectString)
        {
            return Init();
        }

        private string ParameterKey;
        private string OriginalKey;

        public string GetParameterKey()
        {
            return ParameterKey;
        }

        public string GetOriginalKey()
        {
            return OriginalKey;
        }

        public DataSet GetDataSet(string sql)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            DataSet ds = hs.GetDataSet(sql);
            return ds;
        }

        public int SaveDataTable(DataTable dataTable)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            DataSet ds = new DataSet();
            dataTable.DataSet.Tables.Remove(dataTable);
            ds.Tables.Add(dataTable);
            hs.UpdataDataSet(ds);
            dataTable.AcceptChanges();
            return 0;
        }

        public int ExecuteSQL(string sql)
        {
            MFDBResuleInfo info = ExecuteSQL(sql, CommandType.Text,null,30);
            if (info != null)
                return info.Count;
            else
                return -2;
        }

        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFDataParameter[] webParameter;
            Web.MFDBResuleInfo info=null;
           
            if (dataParameters!=null)
            {
                webParameter = ExChangeFromParameters(dataParameters);
                info = hs.ExecuteSQL(sql, ExChangeToCommandType(commandType), webParameter, commandTimeOut);
            }
            else
            {
                info = hs.ExecuteSQL(sql, ExChangeToCommandType(commandType), null, commandTimeOut);
            }



            return ExChangeFromDBResuleInfo(info);

        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            Web.HostServiceSoapClient hs = new Web.HostServiceSoapClient();
            Web.MFDataParameter[] webParameter;
            Web.MFDBResuleDataSetInfo info = null;

            if (dataParameters != null)
            {
                webParameter = ExChangeFromParameters(dataParameters);
                info = hs.ExecuteSQLDataSet(sql, ExChangeToCommandType(commandType), webParameter, commandTimeOut);
            }
            else
            {
                info = hs.ExecuteSQLDataSet(sql, ExChangeToCommandType(commandType), null, commandTimeOut);
            }



            return ExChangeFromDBResuleDataSetInfo(info);
        }


        private Web.CommandType ExChangeToCommandType(CommandType commandType)
        {
            switch (commandType)
            {
                case CommandType.StoredProcedure: return Web.CommandType.StoredProcedure;
                case CommandType.TableDirect: return Web.CommandType.TableDirect;
                case CommandType.Text: return Web.CommandType.Text;
            }
            return Web.CommandType.Text;
        }

        private MFDBResuleDataSetInfo ExChangeFromDBResuleDataSetInfo(Web.MFDBResuleDataSetInfo dbResuleInfo)
        {
            MFDBResuleDataSetInfo info = new MFDBResuleDataSetInfo();
            info.Code = dbResuleInfo.Code;
            info.Count = dbResuleInfo.Count;
            info.DataParameters = ExChangeToParameters(dbResuleInfo.DataParameters);
            info.IsSuccess = dbResuleInfo.IsSuccess;
            info.Message = dbResuleInfo.Message;
            info.ResultDataSet = dbResuleInfo.ResultDataSet;
            return info;
        }

        private MFDBResuleInfo ExChangeFromDBResuleInfo(Web.MFDBResuleInfo dbResuleInfo)
        {
            MFDBResuleInfo info= new MFDBResuleInfo();
            info.Code = dbResuleInfo.Code;
            info.Count = dbResuleInfo.Count;
            info.DataParameters = ExChangeToParameters(dbResuleInfo.DataParameters); 
            info.IsSuccess = dbResuleInfo.IsSuccess;
            info.Message = dbResuleInfo.Message;

            return info;
        }

        private Web.DbType ExChangeToDbType(DbType dbType)
        {
            string name=Enum.GetName(typeof(DbType), dbType);
            return (Web.DbType)Enum.Parse(typeof(Web.DbType), name);
        }

        private DbType ExChangeFromDbType(Web.DbType dbType)
        {
            string name = Enum.GetName(typeof(Web.DbType), dbType);
            return (DbType)Enum.Parse(typeof(DbType), name);
        }

        private Web.ParameterDirection ExChangeToParameterDirection(ParameterDirection parameterDirection)
        {
            string name = Enum.GetName(typeof(ParameterDirection), parameterDirection);
            return (Web.ParameterDirection)Enum.Parse(typeof(Web.ParameterDirection), name);
        }

        private ParameterDirection ExChangeFromParameterDirection(Web.ParameterDirection parameterDirection)
        {
            string name = Enum.GetName(typeof(Web.ParameterDirection), parameterDirection);
            return (ParameterDirection)Enum.Parse(typeof(ParameterDirection), name);

        }

        private MFDataParameters ExChangeToParameters(Web.MFDataParameter[] dataParameters)
        {
            MFDataParameters mfParameter = new MFDataParameters();
            int i = 0;
            foreach (Web.MFDataParameter item in dataParameters)
            {
                MFDataParameter parameter = new MFDataParameter();
                parameter.DbType = ExChangeFromDbType(item.DbType);
                parameter.Direction = ExChangeFromParameterDirection(item.Direction);
                parameter.IsNullable = item.IsNullable;
                parameter.Size = item.Size;
                parameter.SourceColumn = item.SourceColumn;
                //webParameter[i].SourceVersion = item.SourceVersion;
                parameter.Value = item.Value;
                parameter.ParameterName = item.ParameterName;
                mfParameter.Add(parameter);
                i++;
            }
            return mfParameter;
        }

        private Web.MFDataParameter[] ExChangeFromParameters(MFDataParameters dataParameters)
        {
            Web.MFDataParameter[] webParameter = new Web.MFDataParameter[dataParameters.Count];
            int i = 0;
            foreach(MFDataParameter item in dataParameters)
            {
                webParameter[i] = new Web.MFDataParameter();
                webParameter[i].DbType = ExChangeToDbType(item.DbType);
                webParameter[i].Direction = ExChangeToParameterDirection(item.Direction);
                webParameter[i].IsNullable = item.IsNullable;
                webParameter[i].Size = item.Size;
                webParameter[i].SourceColumn = item.SourceColumn;
                //webParameter[i].SourceVersion = item.SourceVersion;
                webParameter[i].Value = item.Value;
                webParameter[i].ParameterName = item.ParameterName;
                i++;
            }
            return webParameter;
        }
    }

    /// <summary>
    /// 通过RemoteService服务获取数据 (待完善)
    /// </summary>
    public class MFRemoteDataService : IDataService
    {
        public bool Init()
        {
            return true;
        }
        public bool Init(string connectString)
        {
            return Init();
        }

        private string ParameterKey;
        private string OriginalKey;

        public string GetParameterKey()
        {
            return ParameterKey;
        }

        public string GetOriginalKey()
        {
            return OriginalKey;
        }

        public DataSet GetDataSet(string sql)
        {
            return null;
        }

        public int SaveDataTable(DataTable dataTable)
        {
            return 0;
        }

        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            return new MFDBResuleInfo();
        }

        public int ExecuteSQL(string sql)
        {
            return 0;
        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            return new MFDBResuleDataSetInfo();
        }

    }

    /// <summary>
    /// 通过WCFService服务获取数据
    /// </summary>
    public class MFWCFDataService : IDataService
    {
        public bool Init()
        {
            return true;
        }
        public bool Init(string connectString)
        {
            return Init();
        }

        private string ParameterKey;
        private string OriginalKey;

        public string GetParameterKey()
        {
            return ParameterKey;
        }

        public string GetOriginalKey()
        {
            return OriginalKey;
        }

        public DataSet GetDataSet(string sql)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            DataSet da = wcf.GetDataSet(sql);
            return da;
        }

        public int SaveDataTable(DataTable dataTable)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            DataSet ds = new DataSet();
            dataTable.DataSet.Tables.Remove(dataTable);
            ds.Tables.Add(dataTable);
            wcf.UpdataDataSet(ds);
            dataTable.AcceptChanges();
            return 0;
        }

        public MFDBResuleInfo ExecuteSQL(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            return wcf.ExecuteSQL(sql, commandType, dataParameters.ToArray(), commandTimeOut);
        }

        public int ExecuteSQL(string sql)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            return wcf.ExecuteSQL(sql,CommandType.Text,null,30).Count;
        }

        public MFDBResuleDataSetInfo ExecuteSQLDataSet(string sql, CommandType commandType, MFDataParameters dataParameters, int commandTimeOut)
        {
            Wcf.WCFServiceClient wcf = new Wcf.WCFServiceClient();
            return wcf.ExecuteSQLDataSet(sql, commandType, dataParameters.ToArray(), commandTimeOut);
        }
    }
}
